<template>
  <div class="user-info">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="手机号" prop="mobile" style="width:300px">
        <el-input v-model="ruleForm.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="ruleForm.timeOfEntry"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <!-- 头像 -->
      <el-form-item label="员工头像">
        <!-- <img :src="ruleForm.staffPhoto"> -->
        <!-- 放置上传图片 -->
        <!-- ref 方法   <upload-img ref="staffPhoto" />  -->
        <upload-img v-model="ruleForm.staffPhoto" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存更新</el-button>
        <el-button @click="$router.back()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
export default {
  data() {
    return {

      ruleForm: {
        id: this.$route.query.id,
        staffPhoto: '',
        mobile: '',
        timeOfEntry: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.loadUserDetailById()
  },
  methods: {
    async loadUserDetailById() {
      const res = await getUserDetailById(this.ruleForm.id)
      console.log('获取原来列表', res)
      // eslint-disable-next-line no-prototype-builtins
      if (res.data.hasOwnProperty('staffPhoto') === false) {
        res.data.staffPhoto = ''
      }
      this.ruleForm = res.data
      // this.$refs.staffPhoto.imageUrl = res.data.staffPhoto
    },
    // 更新
    async hSave() {
      try {
        // const newPhotoUrl = this.$refs.staffPhoto.imageUrl
        // console.log('从子组件中获取 最新上传的头像的地址', newPhotoUrl)
        // this.ruleForm.staffPhoto = newPhotoUrl
        const res = await saveUserDetailById(this.ruleForm)
        console.log('更新个人详情信息', res)
        this.$message.success('操作成功')

        this.$router.back()
      } catch (err) {
        this.$message.error(err.message)
      }
    },
    submitForm() {
      this.$refs.ruleForm.validate(valid => {
        valid && this.hSave()
      })
    }
  }
}
</script>

<style>

</style>
